/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DomainLayer;
import TUILayer.*;
import java.util.ArrayList;
import java.util.Arrays;

        

/**
 *
 * @author Rick
 */
public class AddressBook {

    // create the list of people
    private ArrayList<Person> people;


    public AddressBook()
    {
        // initiate the list of people
        people = new ArrayList<Person>();
    }

    // initiate the class with a single person in the arraylist
    public AddressBook(Person p)
    {
        this();
        people.add(p);
    }

    public AddressBook(Person[] persons)
    {
        this();
        people.addAll(Arrays.asList(persons));
    }


    // return the full list of people
    public Person[] getPeople() {
        return people.toArray(new Person[people.size()]);
    }
    /**
     *
     * @param id
     * @return returnPerson
     */
    public Person getPersonById(int id)
    {
        // initalize the holder person
        Person returnPerson = null;

        // iterate through the list of people and set the
        // person with the requested ID as the holder above
        for(Person p : people)
        {
            if(p.getID() == id)
            {
                // set the correct person as the holder
                returnPerson = p;
            }
        }
        // return the found person
        return returnPerson;
    }

    /**
     * 
     * @param index
     * @return
     */
    public Person getPersonByIndex(int index)
    {
        return people.get(index);
    }

    /**
     *
     * @param p
     * @return
     */
    public boolean addPerson(Person p)
    {
        // trys  to add a person to the arraylist
        try{
           return people.add(p);

        }
        // if an index exception offurs, changes the return message and returns
        // the exception message
        catch(IndexOutOfBoundsException e)
        {
            System.out.println("Caught exception: " + e.getMessage());
        }
        return false;
    }

    /**
     *
     * @param personID
     * @return
     */
    public boolean addPerson(int personID)
    {
        // searches for the correct person
        Person p = getPersonById(personID);

        // trys  to add a person to the arraylist
        try{
           return people.add(p);
        }
        // if an index exception occurs, changes the return message and returns
        // the exception message
        catch(IndexOutOfBoundsException e)
        {
            System.out.println("Caught exception: " + e.getMessage());
        }

        return false;
    }

    /**
     *
     * @param searchPerson
     * @return
     */
    public boolean removePerson(Person searchPerson)
    {
        // set null person as a template
        Person deletePerson = null;

        // search for the person by object
        for(Person p : people)
        {
            if(p == searchPerson)
            {
                // set the correct person as the holder
                deletePerson = p;
            }
        }
        
        // error catching to and deleting the person
        try{
            return people.remove(deletePerson);
        }
        catch(IndexOutOfBoundsException e)
        {
            System.out.println("Caught exception: " + e.getMessage());
        }

        // return the result
        return false;
    }

    /**
     *
     * @param personID
     * @return
     */
    public boolean removePerson(int personID)
    {
        // set null person as a template
        Person deletePerson = null;

        // search for the person by object
        for(Person p : people)
        {
            if(p.getID() == personID)
            {
                // set the correct person as the holder
                deletePerson = p;
            }
        }

        // error catching to and deleting the person
        try{
            return people.remove(deletePerson);
        }
        catch(IndexOutOfBoundsException e)
        {
            System.out.println("Caught exception: " + e.getMessage());
        }

        // return the result
        return false;
    }
    public boolean editPersonStringById(int id, String newField, String newValue) {
        Person p = getPersonById(id);
        String field = "";
        field = newField;
        
        if (field.equals("name")) {
            p.setName(newValue);
        } else
            if(field.equals("address")) {
                p.setAddress(newValue);
            } else
                if (field.equals("city")) {
                    p.setCity(newField);
                } else {
                    return false;
                }
        return true;
    }
    public boolean editPersonIntById(int id, String newField, int newValue) {
        Person p = getPersonById(id);
        String field = "";

        field = newField;

        if (field.equals("postalcode")) {
            p.setPostalCode(newValue);
        } else
            if(field.equals("phone")) {
                p.setPhone(newValue);
            } else {
                return false;
            }

        return true;
    }
}
